package com.forbet.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forbet.demo.mapper.EuropeBankerInfoMapper;
import com.forbet.demo.mapper.PlayInfoMapper;
import com.forbet.demo.mapper.PredictAccuracyMapper;
import com.forbet.demo.po.EuropeBankerInfoPO;
import com.forbet.demo.po.PlayInfoPO;
import com.forbet.demo.po.PredictAccuracyPO;
import com.forbet.demo.vo.IndexVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author:hading
 * @create: 2023-11-24 15:29:22
 * @Description: 前端页面控制Controller
 */
@Controller
public class IndexController {

    //    @Autowired
//    private SuggestionMapper suggestionMapper;
//
    @Autowired
    private PlayInfoMapper playInfoMapper;

    @Autowired
    private PredictAccuracyMapper predictAccuracyMapper;

    @Autowired
    private EuropeBankerInfoMapper europeBankerInfoMapper;

    @RequestMapping(value = "/index")
    public String index(Map<String, Object> map) {
        Date nowDate = new Date();
        LambdaQueryWrapper<PlayInfoPO> playInfoPOLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        查询3小时前的比赛——今天整体结束时间+20分钟的比赛
        playInfoPOLambdaQueryWrapper.between(PlayInfoPO::getPlayingTime, DateUtil.offsetHour(nowDate,-3), DateUtil.offsetHour(nowDate,10));
//        playInfoPOLambdaQueryWrapper.between(PlayInfoPO::getPlayingTime, DateUtil.beginOfDay(DateUtil.parse("2023-12-05")),DateUtil.endOfDay(DateUtil.parse("2023-12-06")));
        playInfoPOLambdaQueryWrapper.orderByAsc(PlayInfoPO::getPlayingTime);
        List<PlayInfoPO> playInfoPOS = playInfoMapper.selectList(playInfoPOLambdaQueryWrapper);
        List<IndexVO> indexVOList = new ArrayList<>();
        BigDecimal correctCount = new BigDecimal("0.00");
        BigDecimal correctCount2 = new BigDecimal("0.00");
        BigDecimal correctCount3 = new BigDecimal("0.00");
        BigDecimal sumCount1 = new BigDecimal("0.00");
        BigDecimal sumCount2 = new BigDecimal("0.00");
        BigDecimal sumCount3 = new BigDecimal("0.00");
        for (PlayInfoPO playInfoPO : playInfoPOS
        ) {
            IndexVO indexVO = new IndexVO();
            LambdaQueryWrapper<PredictAccuracyPO> poLambdaQueryWrapper = new LambdaQueryWrapper<>();
            poLambdaQueryWrapper.eq(PredictAccuracyPO::getPlayInfoId, playInfoPO.getId());
            List<PredictAccuracyPO> predictAccuracyPOS = predictAccuracyMapper.selectList(poLambdaQueryWrapper);
            if (predictAccuracyPOS != null && predictAccuracyPOS.size() > 0) {
                PredictAccuracyPO predictAccuracyPO = predictAccuracyPOS.get(0);
                indexVO.setEuropeResultType(predictAccuracyPO.getEuropeResultType());
                if ("0".equals(predictAccuracyPO.getEuropeResultType())) {
                    correctCount = correctCount.add(BigDecimal.ONE);
                    sumCount1 = sumCount1.add(BigDecimal.ONE);
                }else if("1".equals(predictAccuracyPO.getEuropeResultType())){
                    sumCount1 = sumCount1.add(BigDecimal.ONE);
                }
                indexVO.setAsianResultType(predictAccuracyPO.getAsianResultType());
                if ("0".equals(predictAccuracyPO.getAsianResultType())) {
                    correctCount2 = correctCount2.add(BigDecimal.ONE);
                    sumCount2 = sumCount2.add(BigDecimal.ONE);
                }else if("1".equals(predictAccuracyPO.getAsianResultType())){
                    sumCount2 = sumCount2.add(BigDecimal.ONE);
                }
                indexVO.setBigSmallResultType(predictAccuracyPO.getBigSmallResultType());
                if ("0".equals(predictAccuracyPO.getBigSmallResultType())) {
                    correctCount3 = correctCount3.add(BigDecimal.ONE);
                    sumCount3 = sumCount3.add(BigDecimal.ONE);
                }else if("1".equals(predictAccuracyPO.getBigSmallResultType())){
                    sumCount3 = sumCount3.add(BigDecimal.ONE);
                }
            }
            BeanUtil.copyProperties(playInfoPO, indexVO, CopyOptions.create().ignoreNullValue());
            indexVOList.add(indexVO);
            if (correctCount.compareTo(BigDecimal.ZERO) != 0) {
                String pre = correctCount.divide(sumCount1, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).toPlainString();
                pre = pre.split("\\.")[0];
                map.put("pre", pre);
            }
            if (correctCount2.compareTo(BigDecimal.ZERO) != 0) {
                String pre2 = correctCount2.divide(sumCount2, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).toPlainString();
                pre2 = pre2.split("\\.")[0];
                map.put("pre2", pre2);
            }
            if (correctCount3.compareTo(BigDecimal.ZERO) != 0) {
                String pre3 = correctCount3.divide(sumCount3, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).toPlainString();
                pre3 = pre3.split("\\.")[0];
                map.put("pre3", pre3);
            }
            map.put("list", indexVOList);
        }
        return "index";
    }

}
